/*
 * JuzLogMain.cpp
 *
 *  Created on: 2017年5月26日
 *      Author: orange
 */

#include "JuzLogMain.h"

#include <slog.h>
#include <string>

#include "../public/event/LogFileJuzEvent.h"
#include "../public/event/OnDisableJuzEvent.h"
#include "../public/event/OnEnableJuzEvent.h"

using namespace std;

JuzLogMain *JuzLogMain::mJuzLogMain = NULL;
#define logConfigPath "./logConfig"

Plugin *loadSoPlugin()
{
	return JuzLogMain::getInstance();
}

void JuzLogMain::onEnable() {
}
JuzLogMain::JuzLogMain() {
	SLOG_INIT(logConfigPath);
}

JuzLogMain::~JuzLogMain() {
	SLOG_UNINIT();
}

void JuzLogMain::eventListen(JuzEvent* event) {
	string evName = event->mEventName;

	if(evName == OnEnableJuzEvent::mEventName)
	{
		onEnable();
	}
	else if(evName == LogFileJuzEvent::mEventName)
	{
		LogFileJuzEvent *logEv = (LogFileJuzEvent*)event;
		char *msg = logEv->getMsg();
		int nGra = logEv->getGrade();

		if(nGra >= 5)
		{
			slog_trace(msg);
		}
		else if(nGra == 4)
		{
			slog_debug(msg);
		}
		else if(nGra == 3)
		{
			slog_info(msg);
		}
		else if(nGra == 2)
		{
			slog_warn(msg);
		}
		else if(nGra <= 1)
		{
			slog_error(msg);
		}
	}
}

void JuzLogMain::bindFinsh() {
	registeredEvent(OnEnableJuzEvent::mEventName,100);
	registeredEvent(OnDisableJuzEvent::mEventName,1);
	registeredEvent(LogFileJuzEvent::mEventName,100);
}

JuzLogMain* JuzLogMain::getInstance() {
	if(mJuzLogMain ==NULL)
	{
		mJuzLogMain = new JuzLogMain();
	}
	return mJuzLogMain;
}
